package com.mycom.common.utils;

import java.math.BigDecimal;

public class DoubleUtil {

	/**
	 * 相加
	 */
	public static double add(double a1, double b1) {
		BigDecimal a2 = new BigDecimal(Double.toString(a1));
		BigDecimal b2 = new BigDecimal(Double.toString(b1));
		return a2.add(b2).doubleValue();
	}

	/**
	 * 相减
	 */
	public static double sub(double a1, double b1) {
		BigDecimal a2 = new BigDecimal(Double.toString(a1));
		BigDecimal b2 = new BigDecimal(Double.toString(b1));
		return a2.subtract(b2).doubleValue();
	}

	/**
	 * 相乘
	 */
	public static double mul(double a1, double b1) {
		BigDecimal a2 = new BigDecimal(Double.toString(a1));
		BigDecimal b2 = new BigDecimal(Double.toString(b1));
		return a2.multiply(b2).doubleValue();
	}

	/**
	 * 相除
	 */
	public static double div(double a1, double b1, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("error");
		}
		BigDecimal a2 = new BigDecimal(Double.toString(a1));
		BigDecimal b2 = new BigDecimal(Double.toString(b1));
		return a2.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
